/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.jobmesmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.StuJob;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.StuJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.StuJobSearch;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.StuJobSearchService;

import java.util.List;

/**
 * 勤工视图Controller
 * @author zms
 * @version 2018-04-01
 */
@Controller
@RequestMapping(value = "${adminPath}/jobmesmanage/stuJobSearch")
public class StuJobSearchController extends BaseController {

	@Autowired
	private StuJobSearchService stuJobSearchService;
	@Autowired
	private StuJobService stuJobService;
	@ModelAttribute
	public StuJobSearch get(@RequestParam(required=false) String id) {
		StuJobSearch entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = stuJobSearchService.get(id);
		}
		if (entity == null){
			entity = new StuJobSearch();
		}
		return entity;
	}
	
	@RequiresPermissions("jobmesmanage:stuJobSearch:view")
	@RequestMapping(value = {"list", ""})
	public String list(StuJobSearch stuJobSearch, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<StuJobSearch> page = stuJobSearchService.findPage(new Page<StuJobSearch>(request, response), stuJobSearch);
		List<StuJobSearch> stuJobs = stuJobSearchService.findList(stuJobSearch);
		model.addAttribute("stuJobs",stuJobs);
		model.addAttribute("page", page);
		return "modules/campusinfostu/stuJobSearchList";
	}

	@RequiresPermissions("jobmesmanage:stuJobSearch:view")
	@RequestMapping(value = "detail")
	public String detail(StuJobSearch stuJobSearch, HttpServletRequest request, HttpServletResponse response, Model model) {
		Integer stujobid = Integer.valueOf(stuJobSearch.getJoblinkid());
		stuJobSearch.setJoblinkid(stujobid.toString());
		Page<StuJobSearch> page = stuJobSearchService.findPage(new Page<StuJobSearch>(request, response), stuJobSearch);
		List<StuJobSearch> stuJobs = stuJobSearchService.findList(stuJobSearch);
		model.addAttribute("stuJobs",stuJobs);
		model.addAttribute("page", page);
		return "modules/campusinfostu/stuJobSearchList";
	}
	@RequiresPermissions("jobmesmanage:stuJob:edit")
	@RequestMapping(value = "delete")
	public String delete(StuJob stuJob, RedirectAttributes redirectAttributes) {
		stuJobService.delete(stuJob);
		addMessage(redirectAttributes, "删除报名勤工成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/stuJobSearch/?repage";
	}
	@RequiresPermissions("jobmesmanage:stuJob:edit")
	@RequestMapping(value = "status")
	public String status(StuJob stuJob, RedirectAttributes redirectAttributes) {
		stuJob.setEnrollStatus(CampusinfoOrderStat.DONE.getValue());
		stuJobService.status(stuJob);
		addMessage(redirectAttributes, "审核成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/stuJobSearch/?repage";
	}
}